/*
 * @Author: DUST
 * @Date: 2023-05-29 15:15:14
 * @LastEditors: DUST
 * @LastEditTime: 2023-06-12 15:22:35
 * @FilePath: \koa-node-ts\app\controller\NoteController.ts
 * @Description:
 * undefined
 */

import { Context } from "koa";
import pagination from "../../utils/pagination";
import NoteServeice from "../service/NoteServeice";
import responese from "../../utils/responese";
import NoteValidator, { noteItem } from "../validator/NoteValidator";
import validate from "../../utils/validator";
import TagsService from "../service/TagsService";
import { tagsAddColor } from "../../utils";
import Tags from "../model/Tags";

class NoteController {
  /**
   * @description: 获取笔记列表
   * @param {Context} ctx：请求体
   */
  async getNoteList(ctx: Context) {
    const { pageSize, pageNumber } = pagination.getPageParams(ctx);
    console.log("分页参数：", pageSize, pageNumber);
    // 数据库查询数据
    const { rows, count } = await NoteServeice.getNoteListByPage(
      pageSize,
      pageNumber
    );
    const paginationData = pagination.doPaginate(
      count == 0 ? [] : rows,
      pageSize,
      count,
      pageNumber
    );
    console.log(paginationData);
    responese.success(ctx, paginationData);
  }
  /**
   * @description: 获取笔记列表
   * @param {Context} ctx：请求体
   */
  async getBrowseNoteList(ctx: Context) {
    const { pageSize, pageNumber } = pagination.getPageParams(ctx);
    console.log("分页参数：", pageSize, pageNumber);
    
    // 数据库查询数据
    const { rows, count } = await NoteServeice.getBrowseNoteListByPage(
      pageSize,
      pageNumber
    );
    // console.log(rows);
    for (let index = 0; index < rows.length; index++) {
      const val: noteItem = rows[index];
      if (val.tagIds) {
        val.tagList = await TagsService.getTagByIds(JSON.parse(val.tagIds));
        // 给标签添加颜色
        val.tagList = tagsAddColor(val.tagList);
      }
    }
    const paginationData = pagination.doPaginate(
      count == 0 ? [] : rows,
      pageSize,
      count,
      pageNumber
    );
    console.log(paginationData);
    responese.success(ctx, paginationData);
  }
  /**
   * @description: 根据id查询详情内容
   * @param {Context} ctx：请求体
   */
  async getNoteDetailsById(ctx: Context) {
    const params = ctx.params;
    if (isNaN(params.id)) return responese.errorResponese(ctx, "参数类型错误");
    const row = await NoteServeice.getNoteDetailsByID(params.id);
    if (row?.id) {
      // 更新浏览次数
      row.browseNum += 1;
      NoteServeice.updateBrowseNum(row.id, row.browseNum);
      // 获取标签ids对应的标签信息
      let tagList: Array<Tags> = [];
      if (row?.tagIds)
        tagList = await TagsService.getTagByIds(JSON.parse(row.tagIds));
      // 给标签添加颜色
      tagList = tagsAddColor(tagList);
      // 组装返回数据
      return responese.success(ctx, { ...row.dataValues, tagList: tagList });
    }

    responese.errorResponese(ctx, "未查询到相关内容");
  }

  async addNote(ctx: Context) {
    const { data, msg } = await validate(ctx, NoteValidator.addRules);
    if (msg != null) return responese.errorResponese(ctx, msg);

    const rows = await NoteServeice.addNote(data);
    if (rows?.id) return responese.success(ctx, null);

    responese.errorResponese(ctx, "新增失败");
  }

  async editNote(ctx: Context) {
    const {
      data: { title, content, description, tagIds, userId, id },
      msg,
    } = await validate<noteItem & { id: number }>(ctx, NoteValidator.editRules);
    if (msg) return responese.errorResponese(ctx, msg);
    if (isNaN(id)) return responese.errorResponese(ctx, "传入ID参数类型错误");
    const row = await NoteServeice.editNote(
      { title, content, description, tagIds, userId },
      id
    );
    console.log("编辑：", row);
    if (row) return responese.success(ctx, null, "编辑成功");
    responese.errorResponese(ctx, "编辑失败");
  }

  async deleteNote(ctx: Context) {
    const params = ctx.params;
    if (isNaN(params.id))
      return responese.errorResponese(ctx, "id参数类型错误");

    const row = await NoteServeice.deleteNote(params.id);
    if (row) return responese.success(ctx, null);
    responese.errorResponese(ctx, "删除失败");
  }
}

export default new NoteController();
